package sk.tuke.android4all.messenger;

import java.util.ArrayList;
import java.util.List;

import sk.tuke.android4all.services.Tts;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;

public class SMSHelper {
private Activity mActivity;
	
	public SMSHelper(Activity aActivity_in){
		mActivity = aActivity_in;
	}
	
	public static String getPreviewText(String fullText){
		if(fullText.length() > 50){
			return fullText.substring(0, 50);
		}else{
			return fullText;
		}
	}
	
	public static void readPhoneNumber(Tts aTts_in, String aNumberString){	
		try {
			int stringLenght = aNumberString.length();
			int numbersCount = 0;
			for(int i=0; i< stringLenght; i++){
				if(Character.isDigit(aNumberString.charAt(i))){
					numbersCount ++;
				}
			}	
			if(((float)(float)numbersCount/(float)stringLenght) > 0.2){
				for(int i=0; i<stringLenght; i++){
					aTts_in.sayMessage(String.valueOf(aNumberString.charAt(i)));
					Thread.sleep(450);				
				}
			}else{ //Case it is non-numeric text
				aTts_in.sayMessage(aNumberString);
				Thread.sleep(750);
			}	
		} catch (InterruptedException e) {
			//ignore
		}
	}
	
	public List<Message> getInboxMessages(){
		List<Message> messages = new ArrayList<Message>();
		String SMS_READ_COLUMN = "read";
		String SORT_ORDER = "date DESC";

		Cursor cursor = this.mActivity.getContentResolver().query(
				  Uri.parse("content://sms/inbox"),
		          new String[] {"address", "person", "date", "body" },
		          null,
		          null,
		          SORT_ORDER);
		if (cursor != null) {
				try {
					cursor.moveToFirst();
				do {			
					String address = cursor.getString(0);
					long contactId = cursor.getLong(1);
					String contactId_string = String.valueOf(contactId);
					long timestamp = cursor.getLong(2);
					
					String body = cursor.getString(3);                             
					
					Message newMessage = new Message();
					newMessage.setText(body);
					newMessage.setFrom(address);
					newMessage.setTimestamp(timestamp);
					messages.add(newMessage);
				} while(cursor.moveToNext());
			} finally {
			cursor.close();
			}
		}               
		return messages;
	}
	
	public List<Message> getOutboxMessages(){
		List<Message> messages = new ArrayList<Message>();
		String SMS_READ_COLUMN = "read";
		String SORT_ORDER = "date DESC";

		Cursor cursor = this.mActivity.getContentResolver().query(
				  Uri.parse("content://sms/sent"),
		          new String[] {"address", "person", "date", "body" },
		          null,
		          null,
		          SORT_ORDER);
		if (cursor != null) {
				try {
					cursor.moveToFirst();
				do {			
					String address = cursor.getString(0);
					long contactId = cursor.getLong(1);
					String contactId_string = String.valueOf(contactId);
					long timestamp = cursor.getLong(2);
					
					String body = cursor.getString(3);                             
					
					Message newMessage = new Message();
					newMessage.setText(body);
					newMessage.setFrom(address);
					newMessage.setTimestamp(timestamp);
					messages.add(newMessage);
				} while(cursor.moveToNext());
			} finally {
			cursor.close();
			}
		}               
		return messages;
	}
}
